import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-05-26
 * Time: 14:53
 */
public class LengthOfLIS {
    public static int lengthOfLIS(int[] nums) {
        int n = nums.length;
        ArrayList<Integer> ret = new ArrayList<>();
        ret.add(nums[0]);
        for (int i = 0; i < n; i++) {
            if (nums[i] > ret.get(ret.size() - 1)) {
                ret.add(nums[i]);
            } else {
                int left = 0;
                int right = ret.size() - 1;
                while (left < right) {
                    int mid = (left + right) / 2;
                    if (ret.get(mid) > nums[i]) {
                        right = mid;
                    } else {
                        left = mid + 1;
                    }
                }
                ret.set(left, nums[i]);
            }
        }
        return ret.size();
    }

    public static void main(String[] args) {
        int[] arr = new int[]{10, 9, 2, 5, 3, 7, 101, 18};
        System.out.println(lengthOfLIS(arr));
    }
}
